//
// Created by xsh on 2025/6/3.
//

extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_changeVolEnv(JNIEnv *env, jobject thiz, jint env_type,jdouble value)
{
osc.changeVolEnv(static_cast<Osc3::volEnv>(env_type),value);
}extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_setVolEnv(JNIEnv *env, jobject thiz, jboolean useable)
{
osc.useVolEnv = useable;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_onNote(JNIEnv *env, jobject thiz, jint is_pressed,
jint note) {
if(is_pressed)
{
if(osc.useVolEnv)
{
osc.changeNote(note);
osc.noteOn();// 触发ADSR起始
engine.play();
} else{
osc.changeNote(note);
engine.play();
}
}
else{
osc.noteOff();
engine.stop();
}
}
extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_setVolume(JNIEnv *env, jobject thiz, jdouble vol)
{
LOGD("change vol","%f",vol);
osc.setVol(vol);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_initEngine(JNIEnv *env, jobject thiz)
{
engine.initEngine();
}
// osc3测试代码，引擎将来需要删除，分离逻辑
extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_play(JNIEnv *env, jobject thiz)
{
engine.play();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_stop(JNIEnv *env, jobject thiz)
{
engine.stop();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_changeWave(JNIEnv *env, jobject thiz,jint type)
{
Osc3::WaveType t = static_cast<Osc3::WaveType>(type);
osc.changeWave(t);
}
// never_used;
extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_changeNote(JNIEnv *env, jobject thiz,jint note)
{
osc.changeNote(note);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_destoryEngine(JNIEnv *env, jobject thiz)
{
/*    for(int i=0;i<allPlugin.length,i++)
    {
        dlclose(allPlugin[i]);
    }*/
engine.destoryEngine();
}
// 设置当前乐器
extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_setActiveInstrument(JNIEnv* env, jobject thiz, jint index)
{
// engine.setActiveInstrument(index);
}

// 发送事件
extern "C"
JNIEXPORT void JNICALL
Java_com_xsh_opensltest_SlInstrumentTest_sendEvent(JNIEnv* env, jobject thiz,jstring event_name, jobject value)
{
const char* event = env->GetStringUTFChars(event_name, nullptr);
engine.sendEvent(event, value, env);
env->ReleaseStringUTFChars(event_name, event);
}